import { createRouter, createWebHistory } from 'vue-router'
import { getStore } from '../libs/storage'
import { ElMessage } from 'element-plus'

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  mode: 'history',
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '登录',
      component: () => import('../views/login.vue')
    },
    {
      path: '/home',
      name: '列表',
      component: () => import('../views/home.vue'),
      redirect: "appointment",
      meta : {  // 加一个自定义obj
        requireAuth : true   // 参数 true 代表需要登陆才能进入 A
      },
      children: [
        {
          path: '/appointment',
          component: () => import('../components/appointment.vue'),
        },
        {
          path: '/doctor',
          component: () => import('../components/doctor.vue'),
        },
        {
          path: '/userconfig',
          component: () => import('../components/userconfig.vue'),
        },
        {
          path: '/medicalequipment',
          component: () => import('../components/medicalequipment.vue'),
        },
        {
          path: '/medicine',
          component: () => import('../components/medicine.vue'),
        },
        {
          path: '/account',
          component: () => import('../components/account.vue'),
        },
        {
          path: '/information',
          component: () => import('../components/information.vue'),
        },
        {
          path: '/case',
          component: () => import('../components/case.vue'),
        },
        {
          path: '/bill',
          component: () => import('../components/bill.vue'),
        }
      ]
    },
    {
      path: '/403',
      name: '403',
      component: () => import('../views/403.vue')
    },
    {
      path: '/:pathMatch(.*)',
      name: '404',
      component: () => import('../views/404.vue')
    }

  ],
})
router.beforeEach((to,from,next)=>{
	if(to.meta.requireAuth){
		if(getStore('token')){
			next()
		}else{
      ElMessage({
        message: '请先登录',
        type: 'warning'
      })
			next({path:'/'})
		}
	}else{
		next()
	}
})
export default router
